import {
	request
} from '@/api/request/index.js'
import share from '@/components/share.vue'
import cartModel from '@/api/cart/model.js'

export default {
	components: {
		share
	},
	data() {
		return {
			isMpweixin: false,
			classifylist: [],
			productlist: [],
			classifyid: -1,
			showCartBox: false,
			packagelist: [],
			choosepackname: -1,
			nocontenttext: ''
		}
	},
	onShow() {
		// this.$setTitle("嗨焙鲜生·精选")
	},
	onLoad() {
		// #ifdef H5
		this.isMpweixin = true
		// #endif
		// #ifdef MP-WEIXIN
		this.isMpweixin = false
		uni.hideShareMenu()
		// #endif
		this.getClassifylist()
	},
	onPullDownRefresh() {
		this.getClassifylist()
	},
	methods: {
		getClassifylist() {
			let that = this;
			uni.showLoading({
				mask: true,
				title: '正在加载模块...'
			});
			request.get({
					data: {
						c: 'SwiperController',
						m: 'GetClassifyList',
						city: ''
					}
				})
				.then(res => {
					that.classifylist = [];
					that.productlist = [];
					if (res.Status == "ok") {
						let result = res.Tag,
							classifylist = result.classifyList || [],
							productlist = result.productList || [];
						if (classifylist.length == 0) {
							that.nocontenttext = '---无内容---';
						}
						if(productlist.length == 0){
							that.nocontenttext = '---无内容---';
						}
						that.classifylist = classifylist;
						that.productlist = that.resetProductlist(productlist);
						that.classifyid = classifylist[0]["ClassifyId"];
						that.nocontenttext = '---到底了---';
					} else {
						that.nocontenttext = '---无内容---';
					}
				})
				.catch(error => {
					that.nocontenttext = '---无内容---';
					console.error(error)
				});
		},
		checkProductlistByClassifyid(classifyid = '') {
			let that = this;
			that.classifyid = classifyid;
			uni.showLoading({
				mask: true,
				title: '正在加载模块...'
			});
			request.get({
					data: {
						c: 'SwiperController',
						m: 'GetCommodityList',
						city: '',
						classifyId: classifyid
					}
				})
				.then(res => {
					if (res.Status == "ok") {
						let result = res.Tag;
						that.productlist = that.resetProductlist(result);
						that.nocontenttext = '---到底了---';
					} else {
						that.productlist = [];
						that.nocontenttext = '---无内容---';
					}
				})
				.catch(error => {
					console.error(error)
				});
		},
		resetProductlist(list = []) {
			let newlist = []
			list.map((item) => {
				item.Size = item.Size || "组";
				item.ImageSrc =this.$listImgsrc.replace('{0}',item.CommodityName);
				item.Href = "/pages/detail/detail?cid=" + item.Id + "&pid=" + item.detailsId;
				if (item.DiscountPrice) item.DiscountPrice = parseFloat(item.DiscountPrice.toFixed(2));
				else item.DiscountPrice = 0;
				if (item.Contents && /(<|&lt;)script.*script(>|&gt;)/.test(item.Contents)) item.Contents = ""
				if (item.IsDefaultDisplay) newlist.push(item)
				if(!item.Title || item.Title.trim().length==0) item.Title=''
			});

			return newlist
		},
		goHref(href) {
			console.log(href)
			uni.navigateTo({
				url: href
			})
		},
		openCartBox(item) {
			let that = this;
			let commodityId = item.Id,
				packagename = item.PackageName;
			uni.showLoading({
				mask: true,
				title: '请稍等...'
			});
			request.get({
					data: {
						c: 'SwiperController',
						m: 'GetPackageList',
						commodityid: commodityId
					}
				})
				.then(res => {
					if (res.Status == "ok") {
						let result = res.Tag || [];
						that.showCartBox = true;
						that.choosepackname = packagename;
						let sizelist = []
						result.map((r) => {
							if (r.DiscountPrice) r.DiscountPrice = parseFloat(r.DiscountPrice.toFixed(2));
							else r.DiscountPrice = 0;
							sizelist.push(r.PackageName);
							r.Num = 1;
							r.PackageNameList = sizelist;
							r.detailsId = r.Id;
							r.Id = item.Id;
							r.CommodityName = item.CommodityName;
							r.Title = item.Title;
							r.DeliveryCity = item.DeliveryCity;
							r.DeliveryStore = item.DeliveryStore;
							r.IsFlash = item.IsFlash;
							r.Size = item.Size;
							r.Remarks = item.Remarks
							r.ImageUrl = that.$detailsImgsrc.replace('{0}',item.CommodityName);
						});
						that.packagelist = result;
					} else {

					}
				})
				.catch(error => {
					console.error(error)
				});

		},
		chooseCartBox: function(s) {
			this.choosepackname = s;
		},
		closeCartBox: function() {
			this.showCartBox = false;
		},
		updateCartNum: function(item, type) {
			item.Num = item.Num + type <= 1 ? 1 : item.Num + type;
		},
		addTocart(item) {
			let cartVals = {
				CommodityId: item.Id,
				CommodityName: item.CommodityName,
				CommodityDesc: item.Title || '',
				PackageId: item.detailsId,
				PackageSupplyNo: item.PackageSupplyNo,
				PackageName: item.PackageName,
				Price: item.UnitPrice,
				ZkPrice: item.DiscountPrice,
				DetailsList: [],
				ExpireTime: '',
				DeliveryCity: item.DeliveryCity,
				DeliveryStore: item.DeliveryStore,
				IsFlash: item.IsFlash || false,
				Num: item.Num,
				Size: item.Size
			};

			let r = cartModel.add(cartVals)
			if (r.length > 0) {
				uni.showToast({
					title:'已加入购物车',
					icon: 'none'
				})
			}
		},
	}
}
